# -*- coding: utf-8 -*-
# coding: UTF-8
from scrapy.spiders import CrawlSpider
from scrapy.selector import Selector
from scrapy.http import Request

from ..items import UocoItem


class uocoSpider(CrawlSpider):
    name = 'uocoSpider'
    host = 'http://www.umei.cc/%s'
    start_urls = ['meinvtupian/1.htm']

    def start_requests(self):
        for db_type in self.start_urls:
            yield Request(url=self.host % db_type,
                          callback=self.parse_db_key)

    def parse_db_key(self, response):
        sel = Selector(response)
        lists = sel.xpath('//a[@class="TypeBigPics"]')
        for li in lists:
            url = li.xpath('@href').extract()
            yield Request(url=url[0], callback=self.parse_ph_info)

        next_page = sel.xpath('//div[@class="NewPages"]/ul/li[last()-1]/a/@href').extract()[0]
        if next_page:
            yield Request(url=self.host % next_page, callback=self.parse_db_key)

    def parse_ph_info(self, response):
        item = UocoItem()
        sel = Selector(response)
        name = sel.xpath('//div[@class="ArticleTitle"]/strong/text()').extract()[0]

        next_page = sel.xpath('//div[@class="NewPages"]/ul/child::li[last()]/a/@href').extract()
        next_page = self.host % next_page[0]

        img = sel.xpath('//div[@class="ImageBody"]/p/img/@src').extract()[0]

        if next_page:
            yield Request(url=next_page, callback=self.parse_ph_info)

        item['name'] = name
        item['image_url'] = img
        yield item
